package com.edu.boot.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.edu.boot.common.api.CommonResult;
import com.edu.boot.entity.InterviewProblem;
import com.edu.boot.service.InterviewProblemService;
import com.edu.boot.service.RecuritTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

// 这里假设 CommonResult 类是自定义的统一返回类
import com.edu.boot.common.api.CommonResult;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/interviewProblem")
public class InterviewProblemController {
    
    @Autowired
    private InterviewProblemService interviewProblemService;
    @Autowired
    RecuritTypeService recuritTypeService;
    /**
     * 新增面试题
     * @param interviewProblem 面试题实体
     * @return 统一返回结果
     */
    @PostMapping("/add")
    public CommonResult add(@RequestBody InterviewProblem interviewProblem) {
        interviewProblem.setCreateTime(DateUtil.date());
        interviewProblem.setClicktime((DateUtil.date()));
        boolean result = interviewProblemService.save(interviewProblem);
        if (result) {
            return CommonResult.success("新增成功");
        } else {
            return CommonResult.failed("新增失败");
        }
    }
    
    /**
     * 删除面试题
     * @param id 面试题主键
     * @return 统一返回结果
     */
    @GetMapping("/delete/{id}")
    public CommonResult delete(@PathVariable Integer id) {
        boolean result = interviewProblemService.removeById(id);
        if (result) {
            return CommonResult.success("删除成功");
        } else {
            return CommonResult.failed("删除失败");
        }
    }
    
    /**
     * 修改面试题
     * @param interviewProblem 面试题实体
     * @return 统一返回结果
     */
    @PostMapping("/update")
    public CommonResult update(@RequestBody InterviewProblem interviewProblem) {
        boolean result = interviewProblemService.updateById(interviewProblem);
        if (result) {
            return CommonResult.success("修改成功");
        } else {
            return CommonResult.failed("修改失败");
        }
    }
    
    /**
     * 分页查询面试题
     * @param current 当前页码
     * @param size 每页数量
     * @return 统一返回结果
     */
    @GetMapping("/page")
    public CommonResult page(@RequestParam(name = "currentpage") Integer current,
                             @RequestParam(name = "pagesize") Integer size,
                             @RequestParam(name = "type", required = false) Integer type) {
        Page<InterviewProblem> page = new Page<>(current, size);
        LambdaQueryWrapper<InterviewProblem> queryWrapper =
                new LambdaQueryWrapper<InterviewProblem>()
                .eq(Objects.nonNull(type), InterviewProblem::getType, type)
                .orderByDesc(InterviewProblem::getCreateTime);
        // 分页查询
        IPage<InterviewProblem> resultPage = interviewProblemService.page(page,queryWrapper);
        if (CollectionUtil.isNotEmpty(resultPage.getRecords())) {
            // 获取招聘类型的映射关系（ID -> 类型名称）
            Map<Integer, String> recuritTypeMap = recuritTypeService.getRecuritTypeMap();
            resultPage.getRecords().forEach(item -> {
                item.setTypeMsg(recuritTypeMap.get(item.getType()));
            });
        }
        return CommonResult.success(resultPage);
    }
    
    
    /**
     * 查询全部面试题
     * @return 统一返回结果
     */
    @GetMapping("/list")
    public CommonResult list(@RequestParam(name = "type", required = false) Integer type) {
        QueryWrapper<InterviewProblem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(Objects.nonNull(type), "type", type);
        queryWrapper.orderByDesc("clicknum").orderByDesc("create_time");
        // 查询全部
        List<InterviewProblem> list = interviewProblemService.list(queryWrapper);
        // 获取招聘类型的映射关系（ID -> 类型名称）
        Map<Integer, String> recuritTypeMap = recuritTypeService.getRecuritTypeMap();
        if (CollectionUtil.isNotEmpty(list)) {
            for(int i=0;i<list.size();i++){
                list.get(i).setTypeMsg(recuritTypeMap.get(list.get(i).getType()));
                list.get(i).setIndex(i+1);
            }
        }
        return CommonResult.success(list);
    }
    
    /**
     * 增加浏览量
     * @return 统一返回结果
     */
    @GetMapping("/addClicknum/{id}")
    public CommonResult addClicknum(@PathVariable Integer id) {
        // 查询面试题
        InterviewProblem interviewProblem = interviewProblemService.getById(id);
        interviewProblem.setClicknum(interviewProblem.getClicknum() + 1);
        // 更新面试题
        interviewProblemService.updateById(interviewProblem);
        return CommonResult.success(interviewProblem);
    }
}
